package com.zhongxi.guoyuan.view.personalCenter;

import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.MessageAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.BaseResult;
import com.zhongxi.guoyuan.entity.user.center.MessageData;
import com.zhongxi.guoyuan.entity.user.center.MessageResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.CommonTool;

import org.json.JSONException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SystemMessageActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    private PullToRefreshListView lvList;
    private MessageAdapter adapter;
    private List<MessageData> data = new ArrayList<>();
    private String type = "";
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            position = msg.arg1;
            delete();
        }
    };
    private int position;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_dynamics);
        initTitle();
        tvTitle.setText("最近动态");
        lvList = (PullToRefreshListView) findViewById(R.id.lvList);
        if (getIntent().getExtras() != null) {
            String pushMessage = (String) getIntent().getExtras().get("cn.jpush.android.ALERT");
            if (!TextUtils.isEmpty(pushMessage)) {
                MessageData messageData = new MessageData();
                messageData.setCtime(CommonTool.todayYesterday());
                messageData.setTitle("零利美VIP消息推送");
                messageData.setDescription(pushMessage);
                data.add(messageData);
            }
        }
//        lvList.setMode(PullToRefreshBase.Mode.BOTH);

//        lvList.setOnRefreshListener( this);
    }

    @Override
    protected void initData() {
        super.initData();
        adapter = new MessageAdapter(context, data, handler);
        lvList.setAdapter(adapter);
        getMessage();
        TextView tv = new TextView(context);
        tv.setGravity(Gravity.CENTER);
        tv.setText("亲,您还没有系统消息记录哟!");
        tv.setTextSize(20);
        //当界面为空的时候显示的视图
        lvList.setEmptyView(tv);
    }

    private void getMessage() {
        type = "message";
        getServer(Url.SYS_NOTIFY, null, "", false, null, "get");
    }

    private void delete() {
        type = "delete";

        Map<String, String> map = new HashMap<>();
        map.put("notifyid", data.get(position).getId());
        getServer(Url.DELETE_NOTIFY, map, "", false, null, "post");
    }

    @Override
    protected void processSuccessResult(String response) throws JSONException {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try {
            if (type.equals("message")) {
                MessageResult result = gson.fromJson(response, MessageResult.class);
                if (null != result.getSafeobj() && !"".equals(result.getSafeobj())) {
                    MyApplication.getInstance().setSpValue(result.getSafeobj());
                }
//                data.clear();
                if (null != result.getData() && !"".equals(result.getData())) {
                    data.addAll(result.getData());
                }
//                lvList.onRefreshComplete();
                adapter.notifyDataSetChanged();
            } else if (type.equals("delete")) {
                BaseResponse result = gson.fromJson(response, BaseResponse.class);
                if (null != result.getSafeobj() && !"".equals(result.getSafeobj())) {
                    MyApplication.getInstance().setSpValue(result.getSafeobj());
                }
                showShortToast("删除成功!");
                data.remove(position);
                adapter.notifyDataSetChanged();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        try {
            BaseResponse result = gson.fromJson(response, BaseResponse.class);
            if (null != result.getSafeobj() && !"".equals(result.getSafeobj())) {
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(this);
        lvList.setOnItemClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivBack:
                finish();
                break;
        }
    }

    //最新动态详情
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


    }

//    @Override
//    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
//        getMessage();
//    }
//
//    @Override
//    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
//        getMessage();
//    }
}
